using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;


namespace Rensselaer_Event_Finder.Hibernate.DAO
{
    
     /// <summary>
     /// The service that facilitates interaction between the database
        ///    and the application for the Event class
     /// </summary>
     
    public class EventDAO : GenericNHibernateDAO<Event, int>
    {
       /// <summary>
     /// The service that facilitates interaction between the database
          ///  and the application for the Event class
     /// </summary>
        new public Event save(Event entity)
        {
            int count;
            if (entity.Id == 0)
            {
                Globals.getWebService().saveEvent(entity.Location.Id, entity.Website, entity.Description, entity.Price, entity.Name, entity.Phone, entity.Email, entity.Image_url, entity.StartTime, entity.EndTime);
                Globals.syncDatabase();
                IList<Category> cats = entity.Categories;
                entity = getByNameAndTime(entity.Name,entity.StartTime,entity.EndTime);
                entity.Categories = cats;
            }
            foreach (Category c in entity.Categories)
            {
                 count = Globals.getWebService().saveEventCategory(entity.Id, c.Id);
            }
            Globals.syncDatabase();
            return entity;
        }
        /// <summary>
        /// given the name of the event and the start time, make a query to obtain the event
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>

        public Event getByNameAndTime(string name, DateTime startTime, DateTime endTime)
        {
            IQuery q = Session.GetNamedQuery("Event.byNameAndTime");
            q.SetString("name", name);
            q.SetParameter("startTime", startTime);
            q.SetParameter("endTime", endTime);
            return q.List<Event>()[0];
        }

        /// <summary>
        /// Get all events based on the category
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>

        public IList<Event> getByCategoryId(int categoryId)
        {
            IQuery q = Session.GetNamedQuery("Event.byCategoryId");
            q.SetInt32("categoryId", categoryId);
            return q.List<Event>();
        }

        /// <summary>
        /// Get all events that have the text string in either the description, category, title, or comment
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public IList<Event> getByTextSearch(String text)
        {
            IQuery q = Session.GetNamedQuery("Event.byTextSearch");
            q.SetString("text", "%" + text.ToLower() + "%");
            return q.List<Event>();
        }

        /// <summary>
        ///  Get the soonest upcoming X events 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>

        public IList<Event> getUpcomingEvents(int count)
        {
            IQuery q = Session.GetNamedQuery("Event.upcoming").SetMaxResults(count);
            return q.List<Event>();
        }
        /// <summary>
        ///  Get all events within a certain date time range
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>

        public IList<Event> getEventsInRange(DateTime startDate, DateTime endDate)
        {
            IQuery q = Session.GetNamedQuery("Event.InRange");
            q.SetParameter("startDate", startDate);
            q.SetParameter("endDate", endDate);
            return q.List<Event>();
        }
        /// <summary>
        /// Get all events within a certain date time range and for a certain category
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public IList<Event> getEventsByCategoryInRange(int categoryId, DateTime startDate, DateTime endDate)
        {
            IQuery q = Session.GetNamedQuery("Event.ByCategoryIdInRange");
            q.SetParameter("endDate", endDate);
            q.SetParameter("startDate", startDate);
            q.SetInt32("categoryId", categoryId);
            return q.List<Event>();
        }
        /// <summary>
        /// get all the events starting after the current time that occur at the location with the 
        /// id that is passed in
        /// </summary>
        /// <param name="locationId"></param>
        /// <param name="count"></param>
        /// <returns></returns>

        public IList<Event> getUpcomingEventsByLocationId(int locationId, int count)
        {
            IQuery q = Session.GetNamedQuery("Event.ByLocationIdUpcoming").SetMaxResults(count);
            q.SetInt32("locationId", locationId);
            return q.List<Event>();
        }
    }
}
